Creating a Table and Exporting Selected Records to the Back-End

Description

The following script selects records from the Alpha Sports customer.dbf table and writes them to a new table named "CustomerX" in the Access AlphaSport.mdb database.

Start by defining variables. In this case you need SQL::Connection, SQL::ResultSet, and SQL::TableInfo objects.

dim conn as SQL::Connection
dim rs as SQL::ResultSet
dim ti as SQL::TableInfo
dim source_table as C
dim destination_table as C
dim tbl as P

Define the source and destination tables.

source_table = "C:\Program Files\A5V8\Samples\AlphaSports\customer.dbf"
destination_table = "CustomerX"

Read the properties of the source table. SQL_TableInfoOfDBF() sets the values of the ti SQL::TableInfo object to reflect the properties of source_table.

if .not. SQL_TableInfoOfDBF(source_table, ti) then
    ui_msg_box("Error", "Could not read properties of " + source_table)
    end
end if

Connect to the back-end database. If there is an error, display a message and end the script.

if .not. conn.open("{A5API=Access,FileName='C:\Program Files\A5V8\MDBFiles\Alphasports.mdb',UserName='Admin'}") then
    ui_msg_box("Error", "Could not establish connection" + crlf()+ conn.CallResult.text)
    end
end if

Create the new destination table in the back-end database with the CreateTable() method. If there is an error, display a message, close the connection, and end the script.

ti.Name = destination_table then
if .not. conn.CreateTable(ti)
    ui_msg_box("Error", "Could not create back-end table" + crlf()+ conn.CallResult.text)
    conn.close()
    end
end if

Select the records to copy. Read the data from the source table into the rs SQL::ResultSet object with the ResultSetFromDBF() method.

tbl = table.open(source_table)
query.filter = "bill_state_region = \"MA\""
tbl.query_create()
rs = conn.ResultSetFromDBF(tbl)

Add the selected records to the destination_table with the InsertData() method.

if .not. conn.InsertData("", destination_table, rs)
    ui_msg_box("Error", "Could not insert data into back-end table" + crlf()+ conn.CallResult.text)
    conn.close()
    end
end if

Close the table and the connection.

tbl.close()
conn.close()

Limitations

Desktop Applications Only

See Also